package com.lalifa.googledate.ui.dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.PopupWindow
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.contrarywind.view.WheelView
import com.hjq.widget.layout.SettingBar
import com.lalifa.ext.showTipDialog
import com.lalifa.extension.onClick
import com.lalifa.extension.pk
import com.lalifa.extension.start
import com.lalifa.extension.text
import com.lalifa.extension.toast
import com.lalifa.googledate.R
import com.lalifa.googledate.api.CalendarBean
import com.lalifa.googledate.api.CalendarUp
import com.lalifa.googledate.api.ChooseTimeBean
import com.lalifa.googledate.api.ColorBean
import com.lalifa.googledate.api.CountryBean
import com.lalifa.googledate.api.CountryListBean
import com.lalifa.googledate.api.HolidayUp
import com.lalifa.googledate.ui.activity.AddActivity
import com.lalifa.googledate.ui.activity.WebActivity
import com.lalifa.googledate.ui.adapter.ArrayWheelAdapter
import com.lalifa.googledate.ui.adapter.addNoticeTimeAdapter
import com.lalifa.googledate.ui.adapter.calendarInfoAdapter
import com.lalifa.googledate.ui.adapter.colorAdapter
import com.lalifa.googledate.ui.adapter.countryAdapter
import com.lalifa.googledate.util.CalendarUtils
import com.lalifa.googledate.util.CalendarUtils.CALENDARS_Id
import com.lalifa.googledate.util.Tool
import com.lalifa.googledate.util.UserManager
import com.lalifa.utils.GsonUtil
import com.lalifa.utils.LiveDataBus
import com.lalifa.utils.SPUtil
import com.lalifa.utils.SystemUtil
import com.lalifa.utils.UIKit
import jianj.jianj.app.widght.EmojiView
import jianj.jianj.app.widght.EmojiView1
import per.goweii.layer.core.anim.AnimStyle
import per.goweii.layer.core.anim.NullAnimatorCreator
import per.goweii.layer.core.ktx.onClickToDismiss
import per.goweii.layer.core.ktx.onInitialize
import per.goweii.layer.core.widget.SwipeLayout
import per.goweii.layer.dialog.DialogLayer
import per.goweii.layer.dialog.ktx.backgroundDimDefault
import per.goweii.layer.dialog.ktx.cancelableOnTouchOutside
import per.goweii.layer.dialog.ktx.contentAnimator
import per.goweii.layer.dialog.ktx.contentView
import per.goweii.layer.dialog.ktx.gravity
import per.goweii.layer.dialog.ktx.swipeDismiss


/**
 * 协议弹框
 * @receiver Activity
 * @param callback Function1<[@kotlin.ParameterName] String, Unit>
 */
fun Activity.showAgreeDialog(callback: (isAgree: Boolean) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_agree)
        .cancelableOnTouchOutside(false)
        .gravity(Gravity.CENTER)
        .contentAnimator(NullAnimatorCreator())
        .backgroundDimDefault()
        .addInputMethodCompat(true)
        .onInitialize {
            requireViewById<TextView>(R.id.cancel).setOnClickListener {
                callback.invoke(false)
                dismiss()
            }
            requireViewById<TextView>(R.id.agree).setOnClickListener {
                callback.invoke(true)
                dismiss()
            }
        }.show()
}

/**
 * 设置弹框
 * @receiver Activity
 * @param callback Function1<[@kotlin.ParameterName] String, Unit>
 */
fun Activity.settingDialog(callback: (Int) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_set)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val get = UserManager.get()
            val name = requireViewById<TextView>(R.id.name)
            name.text = get?.username ?: "local account"
            val logout = requireViewById<SettingBar>(R.id.logout)
            logout.isVisible = get != null
            requireViewById<SettingBar>(R.id.toAct).setOnClickListener {
                callback(1)
                dismiss()
            }
            requireViewById<SettingBar>(R.id.toSet).setOnClickListener {
                callback(2)
                dismiss()
            }
            requireViewById<SettingBar>(R.id.toHol).setOnClickListener {
                callback(3)
                dismiss()
            }
            requireViewById<SettingBar>(R.id.toAppInfo).setOnClickListener {
                callback(4)
                dismiss()
            }

            logout.setOnClickListener {
                callback(5)
                dismiss()
            }
        }.show()
}

/**
 * 活动弹框
 * @receiver Activity
 * @param callback Function1<[@kotlin.ParameterName] String, Unit>
 */
fun Activity.showActDialog(callback: () -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_activity)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val arr = arrayListOf<ChooseTimeBean>()
            requireViewById<TextView>(R.id.add1).setOnClickListener {
                dismiss()
            }
            requireViewById<TextView>(R.id.add2).setOnClickListener {
                dismiss()
            }
        }.show()
}

/**
 * 添加活动、任务弹框
 * @receiver Activity
 */
fun AppCompatActivity.addTaskDialog() {
    DialogLayer(this)
        .contentView(R.layout.pop_add_task)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            requireViewById<TextView>(R.id.addTask).setOnClickListener {
                this@addTaskDialog.start(AddActivity::class.java) {

                }
                dismiss()
            }
            requireViewById<TextView>(R.id.addAct).setOnClickListener {
                this@addTaskDialog.start(AddActivity::class.java)
                dismiss()
            }
        }.show()
}

/**
 * 选择日历弹框
 * @receiver Activity
 */
fun Activity.chooseCalendarDialog(data: ArrayList<CalendarBean>, callback: (CalendarBean) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_choose_calendar)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val list = requireViewById<RecyclerView>(R.id.listCalendar)
            val get = UserManager.get()
            var isHave = false
            data.forEach {
                if (it.id == CALENDARS_Id) {
                    isHave = true
                }
            }
            if (get != null && !isHave) {
                data.add(
                    CalendarBean(
                        CALENDARS_Id,
                        get.email,
                        get.email,
                        Color.parseColor(Tool.color),
                        "icon_add5"
                    )
                )
            }
            list.calendarInfoAdapter().apply {
                R.id.changeCalendar.onClick {
                    callback(getModel())
                    dismiss()
                }
            }.models = data
        }.show()
}

/**
 * 选择颜色弹框
 * @receiver Activity
 */
fun Activity.showColorDialog(callback: (ColorBean) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_color)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val data = arrayListOf<ColorBean>()
            val listColor = resources.getStringArray(R.array.color_list)
            listColor.forEachIndexed { index, item ->
                data.add(ColorBean(item, ""))
            }
            val list = requireViewById<RecyclerView>(R.id.list)
            list.colorAdapter().apply {
                R.id.typeName.onClick {
                    callback(getModel())
                    dismiss()
                }
            }.models = data
        }.show()
}

/**
 * 重复规则弹框
 * @receiver Activity
 */
fun Activity.showResumeDialog(callback: (Int) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_resume)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            requireViewById<TextView>(R.id.reNo).onClick {
                callback(0)
                dismiss()
            }
            requireViewById<TextView>(R.id.reDay).onClick {
                callback(Tool.DAY)
                dismiss()
            }
            requireViewById<TextView>(R.id.reWeek).onClick {
                callback(Tool.WEEK)
                dismiss()
            }
            requireViewById<TextView>(R.id.reMouth).onClick {
                callback(Tool.MONTH)
                dismiss()
            }
            requireViewById<TextView>(R.id.reYear).onClick {
                callback(Tool.YEAR)
                dismiss()
            }
            requireViewById<TextView>(R.id.reDiy).onClick {
                showDiyResumeDialog {
                    callback(it)
                }
            }
        }.show()
}

/**
 * 自定义重复规则弹框
 * @receiver Activity
 */
fun Activity.showDiyResumeDialog(callback: (Int) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_diy_resume)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            requireViewById<TextView>(R.id.save).onClick {

                dismiss()
            }
            requireViewById<TextView>(R.id.reDay).onClick {
                callback(Tool.DAY)
            }
            requireViewById<TextView>(R.id.reWeek).onClick {
                callback(Tool.WEEK)
            }
            requireViewById<TextView>(R.id.reMouth).onClick {
                callback(Tool.MONTH)
            }
            requireViewById<TextView>(R.id.reYear).onClick {
                callback(Tool.YEAR)
            }
            requireViewById<TextView>(R.id.reDiy).onClick {
                callback(0)
            }
        }.show()
}

/**
 * 自定义通知时间
 * @receiver Activity
 */
fun Activity.showChooseTimeDialog(list: List<ChooseTimeBean>, callback: (ChooseTimeBean) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_time_add0)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val listView = requireViewById<RecyclerView>(R.id.list)
            listView.addNoticeTimeAdapter().apply {
                R.id.tvTime.onClick {
                    callback(getModel())
                    dismiss()
                }
            }.models = list
            requireViewById<TextView>(R.id.diyTime).onClick {
                showDiyTimeDialog() {
                    callback(it)
                    dismiss()
                }
            }
        }.show()
}

/**
 * 自定义通知时间
 * @receiver Activity
 */
fun Activity.showDiyTimeDialog(callback: (ChooseTimeBean) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_time_add)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val list1 = arrayListOf<String>()
            val list2 = arrayListOf<String>()
            val list3 = arrayListOf<String>()
            val list4 = arrayListOf("1", "2", "3", "4")
            val list5 = arrayListOf(
                getString(R.string.minutes),
                getString(R.string.hour),
                getString(R.string.day),
                getString(R.string.week)
            )
            for (i in 1..600) {
                list1.add("$i")
            }
            for (i in 1..120) {
                list2.add("$i")
            }
            for (i in 1..28) {
                list3.add("$i")
            }
            var position1 = 0
            var position2 = 0
            val wheel1 = requireViewById<WheelView>(R.id.wheel1)
            val wheel2 = requireViewById<WheelView>(R.id.wheel2)
            wheel1.setCyclic(false)
            wheel2.setCyclic(false)
            val adapter1 = ArrayWheelAdapter<Any?>(list1.toList())
            val adapter2 = ArrayWheelAdapter<Any?>(list2.toList())
            val adapter3 = ArrayWheelAdapter<Any?>(list3.toList())
            val adapter4 = ArrayWheelAdapter<Any?>(list4.toList())
            val adapter5 = ArrayWheelAdapter<Any?>(list5.toList())
            wheel1.adapter = adapter1
            wheel2.adapter = adapter5
            wheel1.setOnItemSelectedListener {
                position1 = it
            }
            wheel2.setOnItemSelectedListener {
                position2 = it
                when (it) {
                    0 -> wheel1.adapter = adapter1
                    1 -> wheel1.adapter = adapter2
                    2 -> wheel1.adapter = adapter3
                    3 -> wheel1.adapter = adapter4
                }
            }
            val tz1 = requireViewById<TextView>(R.id.tz1)
            val tz2 = requireViewById<TextView>(R.id.tz2)
            val tz3 = requireViewById<TextView>(R.id.tz3)
            val save = requireViewById<TextView>(R.id.save)
            var type = 0
            tz1.isSelected = true
            tz1.onClick {
                type = 0
                tz1.isSelected = true
                tz2.isSelected = false
                tz3.isSelected = false
            }
            tz2.onClick {
                type = 1
                tz1.isSelected = false
                tz2.isSelected = true
                tz3.isSelected = false
            }
            tz3.onClick {
                type = 2
                tz1.isSelected = false
                tz2.isSelected = false
                tz3.isSelected = true
            }
            save.onClick {
                when (position2) {
                    0 -> {
                        var time = ""
                        var showHour = ""
                        var showM = ""
                        if (list1[position1].toInt() > 60) {
                            val hour = list1[position1].toInt() / 60
                            val yHour = list1[position1].toInt() - hour * 60
                            showHour = "$hour ${getString(R.string.hour)}"
                            if (yHour > 0) showM = "$yHour${getString(R.string.minutes)}"
                            time = "$showHour$showM"
                        } else {
                            time = "${list1[position1].toInt()}${getString(R.string.minutes)}"
                        }
                        callback(
                            ChooseTimeBean(
                                "${getString(R.string.notify0)}$time${getString(R.string.notify1)}",
                                list1[position1].toInt(),
                                type = type
                            )
                        )
                        dismiss()
                    }

                    1 -> {
                        var time = ""
                        var showHour = ""
                        if (list2[position1].toInt() / 24 >= 1) {
                            val day = list2[position1].toInt() / 24
                            val yDay = (list2[position1].toInt()) - day * 24
                            if (yDay > 0) showHour = "$yDay${getString(R.string.hour)}"
                            time = "$day${getString(R.string.day)}$showHour"
                        } else {
                            time = "${list2[position1].toInt()}${getString(R.string.hour)}"
                        }
                        callback(
                            ChooseTimeBean(
                                "${getString(R.string.notify0)}$time${getString(R.string.notify1)}",
                                list2[position1].toInt() * 60,
                                type = type
                            )
                        )
                        dismiss()
                    }

                    2 -> {
                        val time = "${list3[position1].toInt()}${getString(R.string.day)}"
                        callback(
                            ChooseTimeBean(
                                "${getString(R.string.notify0)}$time${getString(R.string.notify1)}",
                                list3[position1].toInt() * 24 * 60,
                                type = type
                            )
                        )
                        dismiss()
                    }

                    3 -> {
                        callback(
                            ChooseTimeBean(
                                "${getString(R.string.notify0)}${list4[position1]}${getString(R.string.week)}",
                                list4[position1].toInt() * 7 * 24 * 60,
                                type = type
                            )
                        )
                        dismiss()
                    }
                }
            }
        }.show()
}

/**
 * 帮助弹框
 * @receiver Activity
 * @param callback Function1<[@kotlin.ParameterName] String, Unit>
 */
fun Activity.showHelpDialog(callback: () -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_help)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {

        }.show()
}

/**
 * 应用信息弹框
 * @receiver Activity
 */
fun AppCompatActivity.showAppDialog() {
    DialogLayer(this)
        .contentView(R.layout.pop_app)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val help1 = requireViewById<SettingBar>(R.id.help1)
            val help2 = requireViewById<SettingBar>(R.id.help2)
            val help3 = requireViewById<SettingBar>(R.id.help3)
            help1.rightText = SystemUtil.getVerName()
            help1.onClick {
                toast(getString(R.string.is_new_v))
            }
            help2.onClick {
                start(WebActivity::class.java) {
                    putExtra("title", getString(R.string.privacy))
                }
            }
            help3.onClick {
                start(WebActivity::class.java) {
                    putExtra("title", getString(R.string.user_agree))
                }
            }
        }.show()
}

/**
 * 添加日历类型弹框
 * @receiver Activity
 */
@SuppressLint("DiscouragedApi", "UseCompatLoadingForDrawables")
fun Activity.showAddTypeDialog(
    id: String = "",
    name: String = "",
    icon: String = "",
    colorIn: Int = Color.parseColor(Tool.color)
) {
    DialogLayer(this)
        .contentView(R.layout.pop_type_add)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            var im = icon
            var color = colorIn
            val listIcon = requireViewById<EmojiView>(R.id.listIcon)
            val listColor = requireViewById<EmojiView1>(R.id.listColor)
            val save = requireViewById<TextView>(R.id.save)
            val chooseIm = requireViewById<AppCompatImageView>(R.id.chooseIm)
            val chooseColor = requireViewById<AppCompatImageView>(R.id.chooseColor)
            val etTitle = requireViewById<AppCompatEditText>(R.id.etCaTitle)
            val del = requireViewById<AppCompatImageView>(R.id.del)
            del.isVisible = id.isNotEmpty()
            etTitle.setText(name)
            if (icon.isNotEmpty()) {
                val resources = UIKit.getResources()
                try {
                    val resourceId: Int = resources.getIdentifier(icon, "drawable", packageName)
                    if (resourceId != 0) {
                        chooseIm.background = resources.getDrawable(resourceId)
                    }
                } catch (e: Exception) {
                    // LogUtils.i("加载本地资源发生错误：" + e.message)
                }
            }
            if (color != -1) {
                chooseColor.setBackgroundColor(color)
            }
            listIcon.setOnEmojiClickListener { _, s ->
                im = s
                val resources = UIKit.getResources()
                try {
                    val resourceId: Int = resources.getIdentifier(s, "drawable", packageName)
                    if (resourceId != 0) {
                        chooseIm.background = resources.getDrawable(resourceId)
                    }
                } catch (e: Exception) {
                    // LogUtils.i("加载本地资源发生错误：" + e.message)
                }
            }
            listColor.setOnEmojiClickListener { _, s ->
                color = Color.parseColor(s)
//                val shape = GradientDrawable()
//                shape.setColor(color)
                chooseColor.setBackgroundColor(color)
            }
            del.onClick {
                showTipDialog(
                    getString(R.string.del_sure),
                    getString(R.string.cancel),
                    getString(R.string.sure)
                ) {
                    getProgress(this@showAddTypeDialog)
                    val d = CalendarUtils.delCalendarAccount(this@showAddTypeDialog, id.toLong())
                    if (d == 1) {
                        LiveDataBus.get().with("calenderList").value = CalendarUp(-1)
                        dismiss()
                    } else {
                        toast(getString(R.string.del_err))
                    }
                    hindProgress()
                }
            }
            save.onClick {
                if (id.isEmpty()) {
                    val text = etTitle.text()
                    if (text.isEmpty()) {
                        toast(getString(R.string.place_input_event_title))
                        return@onClick
                    }
                    if (im.isEmpty()) {
                        toast(getString(R.string.place_choose_icon))
                        return@onClick
                    }
                    if (color == -1) {
                        toast(getString(R.string.place_choose_color))
                        return@onClick
                    }
                    val type = CalendarUtils.addCalendarAccount(
                        this@showAddTypeDialog,
                        "diy#$text", color, im
                    )
                    if (type > 0) {
                        LiveDataBus.get().with("calenderList").value = CalendarUp(1)
                        dismiss()
                    } else {
                        toast(getString(R.string.add_err))
                    }
                } else {
                    val text = etTitle.text()
                    if (text.isEmpty()) {
                        toast(getString(R.string.place_input_event_title))
                        return@onClick
                    }
                    getProgress(this@showAddTypeDialog)
                    val type = CalendarUtils.changeCalendarAccount(
                        this@showAddTypeDialog,
                        id, text, "diy#$text", color, im
                    )
                    if (type > 0) {
                        dismiss()
                    } else {
                        toast(getString(R.string.add_err))
                    }
                    hindProgress()
                }
            }
        }.show()
}

/**
 * 节假日弹框
 * @receiver Activity
 */
fun AppCompatActivity.showHolidayDialog() {
    DialogLayer(this)
        .contentView(R.layout.pop_holiday)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val set = requireViewById<SettingBar>(R.id.set)
            val setCountry = requireViewById<SettingBar>(R.id.setCountry)
            val setZj = requireViewById<SettingBar>(R.id.setZj)
            val name = requireViewById<TextView>(R.id.name)
            val showColor = requireViewById<AppCompatImageView>(R.id.showColor)
            val color = SPUtil.getSp(Tool.holidayColor, Tool.color)
            showColor.setBackgroundColor(Color.parseColor(color))
            val u = UserManager.get()
            name.text =if(u == null) "Local account" else UserManager.get()!!.username.pk()
            set.onClick {
                showColorDialog() {
                    SPUtil.setSp(Tool.holidayColor, it.value)
                    showColor.setBackgroundColor(Color.parseColor(it.value))
                    set.leftText = it.name
                    LiveDataBus.get().with("holidayList").value = HolidayUp(1)
                }
            }
            val get = SPUtil.getSp(Tool.holidayName, Tool.holidayNameJson)
            if (get.isNotEmpty()) {
                val codeList = GsonUtil.json2Obj(get, CountryListBean::class.java)
                val list = arrayListOf<CountryBean>()
                codeList.countryList.forEach {
                    if (it.isPublic == 1) {
                        list.add(it)
                    }
                }
                var s = ""
                if (list.size > 0) {
                    list.forEach {
                        s += it.name + " "
                    }
                }
                setCountry.leftText = if (s == "") getString(R.string.add_country) else s
            }
            setCountry.onClick {
                CountyDialog(this@showHolidayDialog,get).show(
                    this@showHolidayDialog.supportFragmentManager, "dialog")
                dismiss()
            }
            setZj.onClick { }
        }.show()
}


///**
// * 节假日弹框
// * @receiver Activity
// */
//@SuppressLint("NotifyDataSetChanged")
//fun Activity.showCountryDialog(get: String) {
//       DialogLayer(this)
//        .contentView(R.layout.pop_country)
//        .cancelableOnTouchOutside(true)
//        .gravity(Gravity.BOTTOM)
//        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
//        .contentAnimator(AnimStyle.BOTTOM)
//        .backgroundDimDefault()
//        .onClickToDismiss(R.id.closePop)
//        .addInputMethodCompat(false)
//        .onInitialize {
//            val etName = requireViewById<AppCompatEditText>(R.id.etName)
//            val search = requireViewById<TextView>(R.id.search)
//            val list = requireViewById<RecyclerView>(R.id.listCountry)
//            val adapter = list.countryAdapter().apply {
//                R.id.item_county.onClick {
//                    showCountrySetDialog(getModel()) {
//                        if (it != getModel<CountryBean>().isPublic) {
//                            getModel<CountryBean>().isPublic = it
//                            notifyDataSetChanged()
//                            UIKit.postDelayed({
//                                val counterList = CountryListBean(models as List<CountryBean>)
//                                val obj2Json = GsonUtil.obj2Json(counterList)
//                                SPUtil.setSp(Tool.holidayName, obj2Json)
//                                LiveDataBus.get().with("holidayList").value = HolidayUp(1)
//                            }, 500)
//                        }
//                    }
//                }
//            }
//
//            val codeList = GsonUtil.json2Obj(get, CountryListBean::class.java)
//            adapter.models = codeList.countryList
//            search.onClick {
//                val text = etName.text()
//                if (text.isEmpty()) {
//                    toast(getString(R.string.place_input_event_title))
//                    return@onClick
//                }
//                val filter = codeList.countryList.filter { item -> item.name.contains(text) }
//                adapter.models = filter
//            }
//        }.show()
//}

/**
 * 节假日设置弹框
 * @receiver Activity
 */
fun Activity.showCountrySetDialog(holiday: CountryBean, callback: (Int) -> Unit) {
    DialogLayer(this)
        .contentView(R.layout.pop_country_set)
        .cancelableOnTouchOutside(true)
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .contentAnimator(AnimStyle.BOTTOM)
        .backgroundDimDefault()
        .onClickToDismiss(R.id.closePop)
        .addInputMethodCompat(true)
        .onInitialize {
            val name = requireViewById<TextView>(R.id.name)
            val isSet1 = requireViewById<TextView>(R.id.isSet1)
            val isSet2 = requireViewById<TextView>(R.id.isSet2)
            val isSet3 = requireViewById<TextView>(R.id.isSet3)
            val public = holiday.isPublic
            name.text = holiday.name
            isSet1.isSelected = public == 0
            isSet2.isSelected = public == 1
            isSet3.isSelected = public == 1
            isSet1.setOnClickListener {
                callback(0)
                dismiss()
            }
            isSet2.setOnClickListener {
                callback(1)
                dismiss()
            }
            isSet3.setOnClickListener {
                dismiss()
            }
        }.show()
}

//获取显示进度条
internal var dialog: Dialog? = null

@SuppressLint("InflateParams")
fun getProgress(context: Context) {
    dialog = Dialog(context, com.lalifa.base.R.style.MyDialog)
    dialog!!.show()
    dialog!!.window!!.setContentView(
        LayoutInflater.from(context).inflate(com.lalifa.base.R.layout.progress, null)
    )
    dialog!!.setCancelable(true)
}

//隐藏进度条
fun hindProgress() {
    if (dialog != null) {
        dialog!!.dismiss()
    }
}